package com.xueqiu.movieplaymanagersystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xueqiu.movieplaymanagersystem.entity.Movie;
import com.xueqiu.movieplaymanagersystem.mapper.MovieMapper;
import com.xueqiu.movieplaymanagersystem.service.MovieService;
import com.xueqiu.movieplaymanagersystem.utils.UUIDUtils;

import java.util.Date;
import java.util.List;

/**
 * @author xueqiu
 */
@Service
@Transactional
@Slf4j
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieMapper movieMapper;

    /**
     * 发布
     *
     * @param originData 数据源
     * @return 发布成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean publish(Movie originData) {
        originData.setCreateDate(new Date());
        originData.setId(UUIDUtils.uuid());
        int insert = movieMapper.insert(originData);
        log.info("MovieServiceImpl publish originData ----> {}",originData);
        return insert == 1;
    }

    /**
     * 编辑
     *
     * @param originData 数据源
     * @return 编辑成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(Movie originData) {
        int update = movieMapper.update(originData);
        log.info("MovieServiceImpl edit originData ----> {}",originData);
        return update == 1;
    }

    /**
     * 删除
     *
     * @param id id字段
     * @return 删除成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(String id) {
        int delete = movieMapper.deleteById(id);
        return delete == 1;
    }

    /**
     * 批量删除
     *
     * @param idList id字段列表
     * @return 删除条数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIdBatch(List<String> idList) {
        int deleteNum = movieMapper.deleteByIdBatch(idList);
        return deleteNum;
    }

    /**
     * 查询；根据电影ID
     *
     * @param id 电影ID
     * @return 可能为NULL
     */
    @Override
    @Transactional(readOnly = true)
    public Movie findById(String id) {
        Movie originMovie = movieMapper.selectById(id);
        log.info("MovieServiceImpl findById originMovie ----> {}",originMovie);
        return originMovie;
    }

    /**
     * 查询；分页查询
     *
     * @param page  当前页
     * @param limit 每页条数
     * @return 可能为EMPTY
     */
    @Override
    @Transactional(readOnly = true)
    public PageInfo<Movie> findAll(int page, int limit) {
        PageInfo<Movie> resultPageInfo = null;
        try (Page<Movie> originPage = PageHelper.startPage(page, limit)) {
            movieMapper.selectAll();

            List<Movie> originList = originPage.getResult();
            resultPageInfo = new PageInfo<>(originList);
        }
        log.info("MovieServiceImpl findAll resultPageInfo ----> {}",resultPageInfo);
        return resultPageInfo;
    }
}
